package com.boot.bootautoconfig.controller;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.boot.bootautoconfig.entity.StafferInfo;
import com.boot.bootautoconfig.mapper.StafferInfoMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yuchuanchuan
 * @date 2020/9/17
 */
@RestController
public class BootConfigController {
    @NacosValue(value = "${test.name:hello}", autoRefreshed = true)
    private String testName;
    @NacosValue(value = "${spring.datasource.url:jdbc:mysql://localhost:3306/tensquare_qa?characterEncoding=utf8}", autoRefreshed = true)
    private String url;
    @NacosValue(value = "${spring.datasource.username:root}", autoRefreshed = true)
    private String userName;
    @NacosValue(value = "${spring.datasource.password:root}", autoRefreshed = true)
    private String password;
    @Resource
    private StafferInfoMapper stafferInfoMapper;

    @GetMapping("/configDemo")
    public String configDemo(){
        return "配置文件修改，无需重新部署，自动修改：" + testName;
    }

    @GetMapping("/getDataSourceInfo")
    public String getDataSourceInfo(){
        return "数据库地址：" + url + "数据库用户名：" + userName + "数据库密码：" + password;
    }

    @GetMapping("/staferList")
    public Map staferList(){
        Map map = new HashMap();
        try{
            List<StafferInfo> list = stafferInfoMapper.stafferInfoList();
            System.out.println("list集合大小===" + list.size());
            map.put("staferList", list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }
}
